package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.Humiture;
import com.hgxd.entity.res.GetHumitureVo;
import com.hgxd.entity.res.ResHumiture;
import com.hgxd.entity.vo.HumitureTimeVo;
import com.hgxd.entity.vo.HumitureVo;
import com.hgxd.result.Result;

import com.hgxd.service.IHumitureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

import static com.hgxd.result.ResultCodeEnum.PARAMETER_IS_EMPTY;

/**
 * <p>
 * 温湿度表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "温湿度表")
@RestController
@RequestMapping("/api/humiture")
@CrossOrigin
public class HumitureController {
    @Resource(name = "HumitureServiceImpl")
    private IHumitureService humitureService;

    /**
     * 获取最新一条的温湿度数据
     */
    @GetMapping("/getTheLatestData")
    @ApiOperation(value = "获取最新一条的温湿度数据", notes = "获取最新一条的温湿度数据")
    @OperationLogAnnotation(operModule = "温湿度表-获取最新一条的温湿度数据", operType = "查询", operContent = "获取最新一条的温湿度数据")
    public Result<List<Humiture>> getTheLatestData() {
        LambdaQueryWrapper<Humiture> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Humiture::getUpdateTime).last("limit 1");
        List<Humiture> list = humitureService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }

    /**
     * 根据组织名称或者id去获取该组织下设备的最新数据
     */
    @GetMapping("/getEquipmentDataByOrganization/{organizationName}")
    @ApiOperation(value = "根据组织名称或者id去获取该组织下设备的最新数据", notes = "根据组织名称或者id去获取该组织下设备的最新数据")
    @ApiImplicitParam(name = "organizationName", value = "温湿度")
    @OperationLogAnnotation(operModule = "温湿度-根据组织名称或者id去获取该组织下设备的最新数据", operType = "查询", operContent = "根据组织名称或者id去获取该组织下设备的最新数据")
    public Result<List<ResHumiture>> getEquipmentDataByOrganization(@PathVariable String organizationName) {
        List<ResHumiture> resHumitures = humitureService.getEquipmentDataByOrganization(organizationName);
        return Result.ok(resHumitures);
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增温湿度数据", notes = "新增温湿度数据")
    @ApiImplicitParam(name = "humiture", value = "温湿度")
    @OperationLogAnnotation(operModule = "温湿度-新增", operType = "新增", operContent = "新增温湿度")
    public Result<String> insert(@RequestBody Humiture humiture) {
        boolean save = humitureService.save(humiture);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 根据时间和组织名称或者组织id来获取该组织的一天的温湿度
     */
    @PostMapping("getDataByTimeAndOrganizationName")
    @ApiOperation(value = "根据时间和组织名称或者组织id来获取该组织的一天的温湿度", notes = "根据时间和组织名称或者组织id来获取该组织的一天的温湿度")
    @ApiImplicitParam(name = "getHumitureVo", value = "工具类")
    @OperationLogAnnotation(operModule = "温湿度-根据时间和组织名称或者组织id来获取该组织的一天的温湿度", operType = "获取", operContent = "根据时间和组织名称或者组织id来获取该组织的一天的温湿度")
    public Result<List<ResHumiture>> getDataByTimeAndOrganizationName(@RequestBody GetHumitureVo getHumitureVo) {
        List<ResHumiture> resHumitures1 = humitureService.getDataByTimeAndOrganizationName(getHumitureVo);
        return Result.ok(resHumitures1);
    }

    /**
     * 根据时间和设备名称或者设备id来获取该设备的一天的温湿度
     */
    @PostMapping("getDataByTimeAndEquipmentName")
    @ApiOperation(value = "根据时间和设备名称或者设备id来获取该设备的一天的温湿度", notes = "根据时间和设备名称或者设备id来获取该设备的一天的温湿度")
    @ApiImplicitParam(name = "getHumitureVo", value = "工具类")
    @OperationLogAnnotation(operModule = "温湿度-根据时间和设备名称或者设备id来获取该设备的一天的温湿度", operType = "获取", operContent = "根据时间和设备名称或者设备id来获取该设备的一天的温湿度")
    public Result<List<ResHumiture>> getDataByTimeAndEquipmentName(@RequestBody GetHumitureVo getHumitureVo) {
        List<ResHumiture> resHumitures = humitureService.getDataByTimeAndEquipmentName(getHumitureVo);
        return Result.ok(resHumitures);
    }

    /**
     * 根据时间和名称来获取该组织的一天的温湿度,用于环境监测 返回的是毫秒值。
     */
    @PostMapping("getDataToMillisecondByTimeAndName")
    @ApiOperation(value = "根据时间和名称来获取该组织的一天的温湿度,用于环境监测 返回的是毫秒值。", notes = "根据时间和名称来获取该组织的一天的温湿度,用于环境监测 返回的是毫秒值。")
    @ApiImplicitParam(name = "getHumitureVo", value = "工具类")
    @OperationLogAnnotation(operModule = "温湿度-获取", operType = "获取", operContent = "根据时间和名称来获取该组织的一天的温湿度,用于环境监测 返回的是毫秒值。")
    public Result<List<ResHumiture>> getDataToMillisecondByTimeAndName(@RequestBody GetHumitureVo getHumitureVo) throws ParseException {
        List<ResHumiture> resHumitures = humitureService.getDataToMillisecondByTimeAndName(getHumitureVo);
        if (resHumitures.isEmpty()) {
            return Result.fail(null, PARAMETER_IS_EMPTY);
        }
        return Result.ok(resHumitures);

    }


    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除温湿度数据", notes = "批量删除温湿度数据")
    @ApiImplicitParam(name = "ids", value = "id的数组")
    @OperationLogAnnotation(operModule = "温湿度-批量删除", operType = "批量删除", operContent = "批量删除温湿度")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = humitureService.removeByIds(ids);
        return data ? Result.ok("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部数据", notes = "查询全部数据")
    @OperationLogAnnotation(operModule = "温湿度-查询全部", operType = "查询全部", operContent = "查询全部温湿度数据")
    public Result<List<Humiture>> findAll() {
        return Result.ok(humitureService.list());
    }


    /**
     * 分页模糊查询 根据传过来的时间来查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @ApiImplicitParam(name = "humitureVo", value = "温湿度分页工具类")
    @OperationLogAnnotation(operModule = "温湿度-分页模糊查询", operType = "分页模糊查询", operContent = "分页模糊查询温湿度数据")
    public Result<Page<Humiture>> findPage(@RequestBody HumitureVo humitureVo) {
        LambdaQueryWrapper<Humiture> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(!"".equals(humitureVo.getStartTime()), Humiture::getUpdateTime, humitureVo.getStartTime(), humitureVo.getStartTime());
        return Result.ok(humitureService.page(new Page<>(humitureVo.getPageNum(), humitureVo.getPageSize()), lambdaQueryWrapper));

    }

    /**
     * 根据时间区间得到最大温湿度
     *
     * @param humitureTimeVo
     * @return
     */
    @PostMapping("/getMaxByDays")
    @ApiOperation(value = "(曹用)根据时间区间和组织名称得到最大温湿度", notes = "根据时间区间和组织名称得到最大温湿度")
    @ApiImplicitParam(name = "humitureTimeVo", value = "时间区间")
    @OperationLogAnnotation(operModule = "温湿度-根据时间区间和组织名称得到最大温湿度", operType = "查询", operContent = "根据时间区间和组织名称得到最大温湿度")
    public Result<List<List<ResHumiture>>> getMaxByDay(@RequestBody HumitureTimeVo humitureTimeVo) throws ParseException {
        if (humitureTimeVo.getEndingTime() == null || humitureTimeVo.getBeginTime() == null) {
            return Result.fail(null, PARAMETER_IS_EMPTY);
        }
        return Result.ok(humitureService.maxTemperatureHumidityPerDays(humitureTimeVo));

    }

    /**
     * 根据时间区间和设备名称得到最大温湿度
     *
     * @param humitureTimeVo
     * @return
     */
    @PostMapping("/getMaxByDays2")
    @ApiOperation(value = "(陈用)根据时间区间和设备名称得到最大温湿度", notes = "根据时间区间和设备名称得到最大温湿度")
    @ApiImplicitParam(name = "humitureTimeVo", value = "时间区间")
    @OperationLogAnnotation(operModule = "温湿度-根据时间区间和设备名称得到最大温湿度", operType = "查询", operContent = "根据时间区间和设备名称得到最大温湿度")
    public Result<List<List<ResHumiture>>> getMaxByDays2(@RequestBody HumitureTimeVo humitureTimeVo) throws ParseException {
        if (humitureTimeVo.getEndingTime() == null || humitureTimeVo.getBeginTime() == null) {
            return Result.fail(null, PARAMETER_IS_EMPTY);
        }
        return Result.ok(humitureService.maxTemperatureHumidityPerDays2(humitureTimeVo));

    }


}

